<?php

namespace app\admin\model\chase;

use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'orders';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'time_text',
        'status_text',
        'type_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5')];
    }

    public function getTypeList()
    {
        return ['wechat' => __('Type wechat'), 'account' => __('Type account'), 'cash' => __('Type cash'), 'alipay' => __('Type alipay')];
    }


    public function getTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['time']) ? $data['time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

//    protected function setTimeAttr($value)
//    {
//        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
//    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    public function design()
    {
        return $this->hasOne(Design::class, 'id', 'design_id');
    }

    public static function getTodayTotalAmount()
    {
        return self::whereTime('createtime', 'today')->where('status', 3)->sum('total_amount');
    }

    public static function getTotalAmount()
    {
        return self::where('status', 3)->sum('total_amount');
    }

    public static function getCountByStatus($status)
    {
        return self::where('status', $status)->count();
    }
}
